package com.docker.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.docker.context.BaseContext;
import com.docker.dto.UserDTO;
import com.docker.dto.UserLoginDTO;
import com.docker.dto.UserUpdateDTO;
import com.docker.entity.User;
import com.docker.exception.UserLoginException;
import com.docker.exception.UserSelectException;
import com.docker.mapper.UserMapper;
import com.docker.service.UserService;
import com.docker.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> list() {
        return null;
    }

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    public User login(UserLoginDTO userLoginDTO) {
         // 登录逻辑
        String userId = userLoginDTO.getStudentId();
        String password= userLoginDTO.getPassword();

        //根据学号查询学生
        User user = userMapper.getByUserId(userId);
        if (user==null){
            throw new UserLoginException("账号不存在");
        }
        //对密码进行 md5 加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())){
            throw new UserLoginException("密码错误");
        }

        return user;
    }




    @Override
    public void updateUserInfo(UserUpdateDTO userUpdateDTO) {
//        Integer id = BaseContext.getCurrentId();
//        System.out.println("BaseContext的ID:"+BaseContext.getCurrentId());
//        if (id != userUpdateDTO.getId()){
//            throw new UserSelectException("权限不足");
//        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }


    @Override
    public List<User> getUsers(Integer page, Integer pageSize) {
        IPage<User> userIPage = new Page<>();
        userIPage.setCurrent(page);
        userIPage.setSize(pageSize);
        List<User> userList = userMapper.getUsers(userIPage);
//        List<UserVo> userVoList = new ArrayList<>();
//        for (User user : userList){
//            UserVo userVo = new UserVo();
//            BeanUtils.copyProperties(user,userVo);
//            userVoList.add(userVo);
//        }
        return userList;
    }

    @Override
    public void addUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.addUser(user);
    }

    @Override
    public List<User> getUserByParam(String name) {
        List<User> userList = userMapper.getUserByParam(name);
//        System.out.println(userList);
        if (userList.isEmpty())
            return null;
        return userList;
    }

    @Override
    public void delUserById(Integer Id) {
        userMapper.delUserById(Id);
    }
}
